package com.zzuli.practice.controller;

import com.zzuli.practice.entity.*;
import com.zzuli.practice.service.impl.HistoryService;
import com.zzuli.practice.service.impl.LoginService;
import com.zzuli.practice.util.Constant;
import com.zzuli.practice.util.JWTUtils;
import com.zzuli.practice.util.PracticeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@CrossOrigin
@EnableTransactionManagement // 开启事务
public class LoginController implements Constant {

    @Autowired
    private LoginService loginService;

    @Autowired
    private HistoryService historyService;

    // 设置事务传播行为
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(String username, String password, String verifycode, String code, HttpServletRequest request, HttpServletResponse response){
        Result result = new Result();

        // 空值处理
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)){
            result.setStatusCode(FAILURE);
            result.setMessage("账号密码不能为空");
            result.setData(null);
            return result;
        }

        // 验证验证码
        System.out.println("验证码：" + code);

        if (!verifycode.equalsIgnoreCase(code)){
            result.setStatusCode(FAILURE);
            result.setMessage("验证码错误");
            result.setData(null);
            return result;
        }

        // 检查账号密码
        User user = loginService.selectByName(username);
        if (user == null){
            result.setStatusCode(FAILURE);
            result.setMessage("该账号不存在");
            result.setData(null);
            return result;
        }
        // 加密
        if (!user.getPassword().equals(PracticeUtil.md5(password + SECRET))){
            result.setStatusCode(FAILURE);
            result.setMessage("密码错误");
            result.setData(null);
            return result;
        }

        // 登录成功
        result.setStatusCode(SUCCESS);
        result.setMessage("登录成功");

        // 生成token
        String token = JWTUtils.createJwtToken(user.getId().toString(), user.getUsername());
        LoginToken loginToken = new LoginToken();
        loginToken.setId(user.getId());
        loginToken.setUsername(user.getUsername());
        loginToken.setToken(token);
        result.setData(loginToken);

        // 添加登录记录
        History history = new History();
        history.setUsername(username);
        history.setTime(new Date());
        history.setIp(request.getRemoteAddr());
        int i = historyService.addHistory(history);
        if (i != 1){
            result.setMessage("登录成功, 添加记录失败");
        }

        return result;
    }

    // 生成验证码
    @RequestMapping(value = "/kaptcha/{p}", method = RequestMethod.GET)
    public Code getKaptcha(HttpServletResponse response, HttpServletRequest request){

        return loginService.getKaptcha(response, request);
    }

}
